package com.lhl.wx.miniapp.handler;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;

import java.io.File;
import java.util.Map;

/**
 * @className: com.lhl.wx.miniapp.handler.QrcodeHandler
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-12-16 15:13
 **/
@Api(tags = "QrcodeHandler", produces = "QrcodeHandler")
@Slf4j
public class QrcodeHandler extends AbstractHandler{
    @Override
    public WxMaXmlOutMessage handle(WxMaMessage wxMaMessage, Map<String, Object> map, WxMaService wxMaService, WxSessionManager wxSessionManager) throws WxErrorException {
        try {
            final File file = wxMaService.getQrcodeService().createQrcode("123", 430);
            WxMediaUploadResult uploadResult = wxMaService.getMediaService().uploadMedia("image", file);
            wxMaService.getMsgService().sendKefuMsg(
                    WxMaKefuMessage
                            .newImageBuilder()
                            .mediaId(uploadResult.getMediaId())
                            .toUser(wxMaMessage.getFromUser())
                            .build());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return null;
    }
}
